package com.lzb.browser.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.AppUtils;
import com.lzb.browser.BaseAc;
import com.lzb.browser.Content;
import com.lzb.browser.R;
import com.lzb.browser.databinding.ActivitySettingBinding;
import com.lzb.browser.HomeActivity;
import com.lzb.browser.basic.utils.SPUtil;
import com.blankj.utilcode.util.ToastUtils;

public class SettingActivity extends BaseAc<ActivitySettingBinding> implements View.OnClickListener {


    @Override
    protected int onCreate() {
        return R.layout.activity_setting;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void initView() {
        mDataBinding.ivSettingBack.setOnClickListener(view -> {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        });

        mDataBinding.llPatternPassword.setOnClickListener(this);
        mDataBinding.llDownloadLocation.setOnClickListener(this);
        mDataBinding.ivWithoutImage.setOnClickListener(this);
        mDataBinding.ivOpenClosePage.setOnClickListener(this);
        mDataBinding.ivDefaultBrowser.setOnClickListener(this);
        mDataBinding.llDefaultSearchEngine.setOnClickListener(this);
        mDataBinding.llBrowserIdentification.setOnClickListener(this);
        mDataBinding.llClearBrowseData.setOnClickListener(this);
        mDataBinding.llFeedback.setOnClickListener(this);
        mDataBinding.llAgreement.setOnClickListener(this);
        mDataBinding.llPrivacy.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llPatternPassword) {//启动图案密码
            boolean isHave = SPUtil.getBoolean(mContext, Content.isHavePassword, false);
            if (isHave) {
                startActivity(EnablePwdActivity.class);
            } else {
                startActivity(PatternPasswordActivity.class);
            }
        } else if (id == R.id.llDownloadLocation) {//下载位置
            startActivity(DownloadLocationActivity.class);
        } else if (id == R.id.llDefaultSearchEngine) {//默认搜索引擎
            startActivity(SearchEngineActivity.class);
        } else if (id == R.id.ivWithoutImage) {//智能无图
            boolean isImagelessMode = SPUtil.getBoolean(mContext, Content.isImagelessMode, false);
            isImagelessMode = !isImagelessMode;
            SPUtil.putBoolean(mContext, Content.isImagelessMode, isImagelessMode);
            mDataBinding.ivWithoutImage.setImageResource(isImagelessMode ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.llBrowserIdentification) {//浏览器ua标识
            startActivity(BrowserIdentificationActivity.class);
        } else if (id == R.id.llClearBrowseData) {//清除浏览器数据
            startActivity(ClearBrowseDataActivity.class);
        } else if (id == R.id.llFeedback) {//意见反馈
            startActivity(FeedbackActivity.class);
        } else if (id == R.id.llAgreement) {//服务协议
            Intent intent = new Intent(this, WebActivity.class);
            intent.putExtra("url", "https://www.baidu.com");
            intent.putExtra("title", getString(R.string.service_agreement));
            startActivity(intent);
        } else if (id == R.id.llPrivacy) {//隐私政策
            Intent intent = new Intent(this, WebActivity.class);
            intent.putExtra("url", "https://www.baidu.com");
            intent.putExtra("title", getString(R.string.privacy_policy));
            startActivity(intent);
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvCurrenVersion.setText(AppUtils.getAppVersionName());
    }

    @Override
    protected void onResume() {
        super.onResume();
        String browserIdentification = SPUtil.getString(mContext, Content.browseIdentification, getString(R.string.android));
        mDataBinding.tvBrowserIdentification.setText(browserIdentification);
        boolean isEnable = SPUtil.getBoolean(mContext, Content.isEnablePassword, false);
        if (isEnable) {
            mDataBinding.tvPasswordEnable.setText(getString(R.string.enabled));
        } else {
            mDataBinding.tvPasswordEnable.setText(getString(R.string.not_enabled));
        }
    }
}